import { useEffect, useRef, useState } from "react";
export const useDropdownText = (username: string) => {
	const timer = useRef<number | null>(null);
	const [text, setText] = useState("");
	const getString = (name: string) => {
		const time_now = new Date();
		const hour = time_now.getHours();
		let welcome = "";
		if (hour >= 0 && hour < 5) {
			welcome = name + "，夜深了，注意休息哦 ~" + welcome;
		} else if (hour >= 5 && hour < 12) {
			welcome = "上午好，" + name + " ~";
		} else if (hour >= 12 && hour <= 18) {
			welcome = "下午好，" + name + " ~";
		} else {
			welcome = "晚上好，" + name + " ~";
		}
		return welcome;
	};
	useEffect(() => {
		setText(getString(username));
		if (timer.current) {
			clearInterval(timer.current);
			timer.current = null;
		}
		timer.current = window.setInterval(() => {
			setText(getString(username));
		}, 1000 * 1);
	}, [username]);
	return text;
};
